/*
 * ElementImpl.cpp
 *
 *  Created on: Apr 19, 2009
 *      Author: wrice
 */

#include "DOMCoreImp.h"
namespace DOMCoreImp {

ElementImp::ElementImp(DOMString nodeName, DocumentImp * ownerDocument)
: NodeImp(nodeName, ownerDocument)
, attributes_( new NamedAttrMapImp( this ) )
{
}

unsigned short ElementImp::getNodeType() {
	return Node::ELEMENT_NODE;
}

DOMString ElementImp::getNodeValue() throw(DOMExceptionPtr) {
	return "";
}

void ElementImp::setNodeValue(DOMString) throw(DOMExceptionPtr) {
	throw DOMExceptionPtr(new DOMExceptionImp(DOMException::NOT_SUPPORTED_ERR));
}


DOMString ElementImp::getTagName() { return getNodeName(); }

bool ElementImp::hasAttributes() {
	return getAttributes()->getLength() != 0;
}

bool ElementImp::hasAttribute(DOMString name) {
	return( NULL != getAttributeNode(name) );
}

NamedAttrMap * ElementImp::getAttributes() {
	return NamedAttrMapPtr::GetPointer(attributes_);
}

DOMString ElementImp::getAttribute(DOMString name) {
	Attr * attr = getAttributeNode(name);
	if( NULL == attr ) return "";
	return attr->getNodeValue();
}

void ElementImp::setAttribute(DOMString name, DOMString value) throw(DOMExceptionPtr) {
//	if( NULL == getOwnerDocument() )
//		throw DOMExceptionPtr(new DOMExceptionImp(DOMException::INVALID_STATE_ERR));

	DocumentImp * doc = getOwnerDocumentImp();
	Attr * newAttr = doc->createAttributeImp(name);
	newAttr->setNodeValue(value);
	setAttributeNode(newAttr);
}

void ElementImp::removeAttribute(DOMString name) throw(DOMExceptionPtr) {
	getAttributes()->removeNamedItem(name);
}

Attr * ElementImp::getAttributeNode(DOMString name) {
	return getAttributes()->getNamedItem(name);
}

Attr * ElementImp::setAttributeNode(Attr * newAttr) throw(DOMExceptionPtr) {
	Attr * rst = getAttributes()->setNamedItem(newAttr);
	dynamic_cast< NodeImp * >( newAttr )->setParentNode( this );
	return rst;
}

Attr * ElementImp::removeAttributeNode(Attr * oldAttr) throw(DOMExceptionPtr) {
	removeAttribute(oldAttr->getName());
	return oldAttr;
}

NodeList * ElementImp::getElementsByTagName(DOMString name) {
	// TODO
	return NULL;
}

}
